package com.sg.hms.controller;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sg.hms.Model.Getpath;
import com.sg.hms.Model.User;
import com.sg.hms.Service.RoomService;
import com.sg.hms.ServiceImpl.RoomServiceImpl;
/***
 * 修改房间的器材数量和状态
 * @author 吴磊
 *
 */
public class UpdateRoomController extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("UTF-8");
		RoomService room=new RoomServiceImpl();
		int rId=Integer.parseInt(req.getParameter("rId"));
		String rName=req.getParameter("rName");
		int eId=Integer.parseInt(req.getParameter("eId"));
		String eName=req.getParameter("eName");
		int count=Integer.parseInt(req.getParameter("count"));
		String state=req.getParameter("state");
		String string =room.getString(rId, eId);
		User user=(User) req.getSession().getAttribute("currentuser");
		int uid=user.getuId();
		String fpath=Getpath.getpath1();
		StringBuffer stringBuffer=new StringBuffer(fpath+"/Update");
		stringBuffer.append(String.valueOf(uid));
		Date date=new Date();
		SimpleDateFormat sim=new SimpleDateFormat("yyyyMMddHHmmss");
		String now=sim.format(date);
		stringBuffer.append(now);
		stringBuffer.append(".txt");
		String path=stringBuffer.toString();
		File file=new File(path);
		file.createNewFile();
		String old="修改前："+"房间编号："+rId+"门牌号："+rName+"器材编号："+eId+"器材名称："+eName+string;
		String news="修改后："+"房间编号："+rId+"门牌号："+rName+"器材编号："+eId+"器材名称："+eName+"数量："+count+"状态："+state;
		FileWriter writer=new FileWriter(file, true);
		writer.write(old);
		writer.write("\r\n");
		writer.write(news);
		writer.flush();
		writer.close();
		room.updateRoom(rId, eId, count, state);
		resp.sendRedirect("allroom");
	}

}
